﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace YouDaoNotesCodes.CSharp_highLevel
{
    class Type_enum
    {
        #region enum key - Value
        /// <summary>
        /// 获取enum参数
        /// </summary>
        public void getEnumPare()
        {
            #region Exp
            //读取方式1
            int para1 = (int)Exp.UP;// 1
            string para1_1 = Exp.UP.ToString();//UP

            //读取方式2
            string para2 = Enum.GetName(typeof(Exp), 1);// UP
            //para2获取方式可用Array
            Array ar2 = Enum.GetNames(typeof(Exp));
            string re_para2 = ar2.GetValue(1).ToString();
            //or
            string[] re_para2_ = Enum.GetNames(typeof(Exp));

            //读取方式3
            string para3 = Enum.GetValues(typeof(Exp)).GetValue(1).ToString();// UP
            //para3获取方式可用Array
            Array ar = Enum.GetValues(typeof(Exp));
            string para = ar.GetValue(1).ToString();//UP

            //读取方式4
            int k = 1;
            string para4 = Enum.Parse(typeof(Exp), k.ToString()).ToString();//UP

            int para5 = (int)Enum.Parse(typeof(Exp), "UP");// 1
            #endregion

            #region MasterWork
            //输出方式一：GetName
            Console.WriteLine("我是四大名著之一的：" + Enum.GetName(typeof(MasterWork), 1));  //西游记（是哪个名著由值获取）


            //输出方式二：GetNames
            string[] array1 = Enum.GetNames(typeof(MasterWork));
            Console.WriteLine("我是四大名著之一的：" + array1[2]);   //红楼梦（是哪个名著由值获取）

            //输出方式三：GetValues
            Array array2 = Enum.GetValues(typeof(MasterWork));
            Console.WriteLine("我是四大名著之一的：" + array2.GetValue(3));  //三国演义（是哪个名著由值获取）

            //输出方式四：GetUnderlyingType
            Type t = Enum.GetUnderlyingType(typeof(MasterWork));
            Console.WriteLine("我输出的是值类型：" + t);       //输出 Int32

            //输出方式五：由值获取内容
            int i = 0;
            string Name = Enum.Parse(typeof(MasterWork), i.ToString()).ToString();     //搜索指为0  Name="水浒传"
            Console.WriteLine("我是四大名著之一的：" + Name);

            //输出方式六：由内容获取值
            string Name2 = "红楼梦";
            int j = Convert.ToInt32(Enum.Parse(typeof(MasterWork), Name2));     //查内容为“红楼梦” 值j=2
            Console.WriteLine("我是《红楼梦》对应的值序号：" + j);

            Console.Write("上述可直接取值：" + (int)MasterWork.红楼梦);

            #endregion
        }


        /// <summary>
        /// 枚举列表中的每个符号代表一个整数值，一个比它前面的符号大的整数值。默认情况下，第一个枚举符号的值是 0.
        /// </summary>
        enum MasterWork
        {
            西游记 = 1,
            红楼梦 = 2,
            三国演义 = 3,
            水浒传 = 0
        };

        enum Exp
        {
            UP = 1,
            RIGHT = 2,
            DOWN = 3,
            LEFT = 4,
            blow = 0,
        };
        #endregion


        #region enum key-value-description
        public enum MyColors
        {
            [Description("yuk!")]
            LightGreen = 0x012020,
            [Description("nice :-)")]
            VeryDeepPink = 0x123456,
            [Description("so what")]
            InvisibleGray = 0x456730,
            [Description("no comment")]
            DeepestRed = 0xfafafa,
            [Description("I give up")]
            PitchBlack = 0xffffff,
            [Description("table")]
            test = 1,
            [Description("table_entry")]
            testy = 0x10
        }
        public static string GetDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                  (DescriptionAttribute[])fi.GetCustomAttributes(
                  typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
        }
        //调用：Type_enum.GetDescription(YouDaoNotesCodes.CSharp_type.Type_enum.MyColors.LightGreen)
        #endregion
    }
}
